/**
 * 
 */
package com.mustafaiev.tair.cts.facade;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.dozer.Mapper;

import com.mustafaiev.tair.cts.dao.IPurchaseDAO;
import com.mustafaiev.tair.cts.dto.PurchaseDTO;
import com.mustafaiev.tair.cts.exeption.DataNotRemovedException;
import com.mustafaiev.tair.cts.exeption.DataNotStoredException;
import com.mustafaiev.tair.cts.model.Purchase;

/**
 * @author tair
 * 
 */
public class PurchaseFacade implements IPurchaseFacade {

	private final Mapper mapper;

	private final IPurchaseDAO purchaseDao;

	private static final Logger LOGGER = Logger.getLogger(PurchaseFacade.class);

	public PurchaseFacade(final IPurchaseDAO purchaseDao, final Mapper mapper) {
		this.purchaseDao = purchaseDao;
		this.mapper = mapper;
	}

	@Override
	public void save(final PurchaseDTO dto) {
		final Purchase purchase = this.mapper.map(dto, Purchase.class);
		try {
			this.purchaseDao.doSave(purchase);
		} catch (final DataNotStoredException e) {
			LOGGER.error(e);
		}
	}

	@Override
	public void saveBatch(final List<PurchaseDTO> purchases) {
		final List<Purchase> list = new ArrayList<Purchase>();
		for (final PurchaseDTO purchaseDTO : purchases) {
			final Purchase purchase = this.mapper.map(purchaseDTO,
					Purchase.class);
			list.add(purchase);
		}
		try {
			this.purchaseDao.saveBatch(list);
		} catch (final DataNotStoredException e) {
			LOGGER.error(e);
		}
	}

	@Override
	public void delete(final PurchaseDTO purchaseDTO) {
		try {
			this.purchaseDao.doDeleteById(purchaseDTO.getId());
		} catch (final DataNotRemovedException e) {
			LOGGER.error(e);
		}
	}

	@Override
	public void deleteBatch(final List<PurchaseDTO> purchases) {
		final List<Purchase> list = new ArrayList<Purchase>();
		for (final PurchaseDTO purchaseDTO : purchases) {
			final Purchase purchase = this.mapper.map(purchaseDTO,
					Purchase.class);
			list.add(purchase);
		}
		try {
			this.purchaseDao.deleteBatch(list);
		} catch (final DataNotRemovedException e) {
			LOGGER.error(e);
		}
	}

	@Override
	public List<PurchaseDTO> retrievePurchases() {
		final List<Purchase> purchases = this.purchaseDao.retrievePurchases();
		final List<PurchaseDTO> purchaseDtos = new ArrayList<PurchaseDTO>();

		for (final Purchase purchase : purchases) {
			final PurchaseDTO purchaseDTO = this.mapper.map(purchase,
					PurchaseDTO.class);
			purchaseDtos.add(purchaseDTO);
		}
		return purchaseDtos;
	}

	@Override
	public PurchaseDTO retrievePurchase(final Long id) {
		final Purchase purchase = this.purchaseDao.retrievePurchase(id);
		final PurchaseDTO dto = this.mapper.map(purchase, PurchaseDTO.class);
		return dto;
	}

	@Override
	public List<PurchaseDTO> retrievePurchasesForBasket(final Long basketId) {
		final List<Purchase> purchases = this.purchaseDao
				.retrievePurchasesForBasket(basketId);
		final List<PurchaseDTO> purchaseDtos = new ArrayList<PurchaseDTO>();
		for (final Purchase purchase : purchases) {
			final PurchaseDTO purchaseDTO = this.mapper.map(purchase,
					PurchaseDTO.class);
			purchaseDtos.add(purchaseDTO);
		}
		return purchaseDtos;
	}

}
